package com.hanlin.beforejavacodesummary.controller;


import cn.hutool.core.lang.Assert;
import com.hanlin.beforejavacodesummary.pojo.otherentry.po.KafkaLParamDO;
import com.hanlin.beforejavacodesummary.pojo.otherentry.po.TokenCheckResponse;
import com.hanlin.beforejavacodesummary.pojo.otherentry.po.TokenDO;
import com.hanlin.beforejavacodesummary.pojo.otherentry.vo.TokenVO;
import com.hanlin.beforejavacodesummary.service.XmlDownLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: lijing
 * @since: 2025-10-28
 * @description:
 */

@RestController
//@RequestMapping("/v1")
public class XmlDownLoadController {

    @Autowired
    private XmlDownLoadService xmlDownLoadService;

    // 模拟的 本地 触发 外部 文件下载
    @GetMapping("/downXml")
    public String downXml() throws IOException {
        Map<String,String> map = new HashMap<>();
        //换成你要保存的地址
        map.put("savePath","E:\\personCode\\LiJingJavaCodeAddress\\beforeJavaCodeSummary\\src\\main\\resources\\downloadxml\\");

        String filePath = xmlDownLoadService.downXml(map);
        return filePath;

    }

    // 模拟 外部接口      被调用 下载 xml文件 的 有参数

//    @GetMapping("/endpoint")
//    public ResponseEntity<Resource> doGet(@RequestBody Map<String,Object> map) {
//
//        System.out.println(map.toString());
//        map.put("failPath","E:\\personCode\\LiJingJavaCodeAddress\\beforeJavaCodeSummary\\src\\main\\resources\\templates\\");
//        map.put("fileName","1.xml");
//
//        return xmlDownLoadService.mockXmlEndpoint(map);
//    }

    // 模拟 外部接口      被调用 下载 xml文件 的 没有参数
    @GetMapping("/endpoint")
    public ResponseEntity<Resource> doGet() {
        Map<String, Object> map = new HashMap<>();
        System.out.println(map.toString());
        map.put("failPath","E:\\personCode\\LiJingJavaCodeAddress\\beforeJavaCodeSummary\\src\\main\\resources\\templates\\");
        map.put("fileName","1.xml");

        return xmlDownLoadService.mockXmlEndpoint(map);
    }


    // 模拟的 外部接口 返回 token接口
    @PostMapping("/auth-service/oauth/token")
    public TokenDO authToken(@RequestBody TokenVO tokenVO) {

        Assert.notBlank(tokenVO.getGrant_type(),"Token类型不能为空");
        Assert.notBlank(tokenVO.getScope(),"信号索引键不能为空");
        Assert.notBlank(tokenVO.getClient_id(),"客户端账号不能为空");
        Assert.notBlank(tokenVO.getClient_secret(),"客户端密码不能为空");

        TokenDO tokenDO = xmlDownLoadService.getToekn();
        return tokenDO;

    }


    // 模拟的 外部接口 检验 token 是否有效
    @PostMapping("/auth-service/oauth/check_token")
    public TokenCheckResponse checkAuthToken() {

        int randomNum = (int) (Math.random() * 100) + 1;

//        if (randomNum % 2 == 0) {
//            return TokenCheckResponse.success();
//        }else {
//            return TokenCheckResponse.error();
//        }

        return TokenCheckResponse.success();
    }



    //模拟 本地 接收 kafka 消息
    @PostMapping("/mockKafKa")
    public String mockKafKa(@RequestBody KafkaLParamDO kafkaLParamDO) throws Exception {

        String fileId = kafkaLParamDO.getFileId();
        String fileName = kafkaLParamDO.getFileName();
        String timestamp = kafkaLParamDO.getTimestamp();

        String filePath = xmlDownLoadService.mockKafkaDealData(kafkaLParamDO);


        return filePath == null ? "参数不全或提供参数未则正确下载文件" : filePath;
    }


}
